package com.dc.free.module.synchronization_common_peson.service;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dc.free.module.hjsfzm.db.model.VWxHjsfzm;
import com.dc.free.module.hjsfzm.service.VWxHjsfzmService;
import com.dc.free.module.synchronization_common_peson.db.model.SynchronizationCommonPeson;
import com.dc.free.util.PersonUtil;
import com.jfinal.kit.StrKit;


public class SynchronizationCommonPesonScanner {
	
	//扫描间隔(单位毫秒)
	private static volatile int scanIntervalWithMsec = 1 * 1000;
	private static final ExecutorService service = Executors.newFixedThreadPool(20);
	//启动标识
	private static volatile boolean isRunning;
	
	private static void handler(String id, String jsonStr) {
		JSONObject json = JSON.parseObject(jsonStr);
		String gmsfhm = json.getString("gmsfhm");
		boolean hasZP = json.getBooleanValue("zp");
		JSONObject response = null;
		
		if(StrKit.notBlank(gmsfhm)) {
			//查询舟山常口库，zp字段转成xp字段
			VWxHjsfzm vWxHjsfzm = new VWxHjsfzm().setGmsfhm(gmsfhm);
			List<VWxHjsfzm> vWxHjsfzms = VWxHjsfzmService.list(vWxHjsfzm);
			if(vWxHjsfzms !=null && vWxHjsfzms.size()>0) {
				response = new JSONObject();
				JSONArray arr = JSON.parseArray(JSON.toJSONString(vWxHjsfzms));
				response.put("data", arr);
			}
			
			if(response  == null) response = PersonUtil.query(gmsfhm.split(","));
			
			JSONArray arr = response.getJSONArray("data");
			//数据字段适配
			for(int i=0; i<arr.size(); i++) {
				JSONObject temp = arr.getJSONObject(i);
				
				if(StrKit.notBlank(temp.getString("sfzh"))) {
					temp.put("gmsfhm", temp.getString("sfzh"));
					temp.remove("sfzh");
				}
				
				if(StrKit.notBlank(temp.getString("xp"))) {
					temp.put("zp", temp.getString("xp"));
					temp.remove("xp");
				}
				if(!hasZP) temp.remove("zp");
				
				if(StrKit.notBlank(temp.getString("zzxz"))) {
					temp.put("hjdz", temp.getString("zzxz"));
					temp.remove("zzxz");
				}
				
				if("1".equals(temp.getString("xb"))) temp.put("xb", "男");
				if("2".equals(temp.getString("xb"))) temp.put("xb", "女");
			}
			
		}
		
		//避免空指针错误
		if(response == null) response = new JSONObject();
		SynchronizationCommonPeson.dao.findById(id)
		.setStatus(1)
		.setResponse(response.toJSONString())
		.update();
	}
	
	/**
	 * 更新至待处理状态9
	 * @param list
	 */
	private static void updateWithUnhandlerStatus(List<SynchronizationCommonPeson> list) {
		StringBuffer ids = new StringBuffer();
		
		for(SynchronizationCommonPeson temp : list) {
			if(ids.length()==0) {
				ids.append(temp.getId());
			}
			else {
				ids.append(",").append(temp.getId());
			}
		}
		
		if(ids.length() > 0)
			SynchronizationCommonPesonService.update(new SynchronizationCommonPeson().setStatus(9).setId(ids.toString()));
		
	}
	
	/**
	 * 启动扫描器
	 */
	final public static void start() {
		isRunning = true;
		
		//扫描
		service.execute(new Runnable() {
			@Override
			public void run() {
				while(isRunning) {
					service.execute(new Runnable() {
						@Override
						public void run() {
							SynchronizationCommonPeson synchronizationCommonPeson = new SynchronizationCommonPeson();
							synchronizationCommonPeson.setCode("person");
							synchronizationCommonPeson.setStatus(0);
							List<SynchronizationCommonPeson> list = SynchronizationCommonPesonService.list(synchronizationCommonPeson);
							updateWithUnhandlerStatus(list);
							
							for(final SynchronizationCommonPeson temp : list) {
								service.execute(new Runnable() {
									@Override
									public void run() {
										handler(temp.getId(), temp.getRequest());
										
									}
								});
							}
						}
					});
					
					try {
						Thread.sleep(scanIntervalWithMsec);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		
	}
	
	/**
	 * 停止扫描器
	 */
	final public static void stop() {
		isRunning = false;
	}
}